define(function(require, exports){
	var pubjs = require('pubjs');
	var util	= require('util');
	var grid  = require('@base/highGrid');
	var $ = require('jquery');
	var common = require('modules/common');

	var auditUtil  = require('../auditUtil');
	var HighGrid = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
						url: 'creativeAudit/auditList',
						getAptitudeInfoUrl:'creativeAudit/getAptitudeInfo',
						param: {
							'IsDeleted': false,
							'idType':config.idType
						},
						cols: [
							{type:"select"},
							{name:'_id',text:"创意Id",width:80,align:'center',render: 'renderId',sort:true},
							{name:'Name',text:"创意名称",width:300,align:'left',render:'renderCreativeName'}
						],
						metrics:[
							{name:'SweetyId',text:"创意包ID",width:60,align:'center',render: 'renderId'},
							{name:'SweetyName',text:"创意包名称", render: 'renderSweetyName',align:'left'},
							{name:'Status',align:'left',text:"总状态",width:80,render:'renderStatus',desc:LANG('蓝：审核中;绿：通过;拒绝：红')},
							{name:'AuditList',align:'left',text:"渠道审核状态",render:'renderSubStatus',desc:LANG('蓝：审核中;绿：通过;红：渠道拒绝;灰：预审相关')},
							{name:'CreateUserId',text:"客户ID",width:60,align:'center',render: 'renderId'},
							{name:'CreateUserName',align:'left',text:"客户账号", render: 'renderName'},
							{name:'CreateTime',align:'left',text:"上传时间",render:'renderTime'}
						],
						reqType:'websocket',
						eventDataLoad: true,
						hasBatch: true,
						hasTab: false,
						hasRefresh: true,
						hasExport: false,
						hasSelect: false,
						hasSwitch: false,
						hasAmount: false,
						hasMenu: true,
						auto_load: true,
						key:config.idType? 'AuditId':'_id',
						pager: {'sizeTypes':[10,20,50,100,200,300,400,500]},
						gridName: 'creativeAudit',
						listRowHoverKey: 'data-index'
					}
			);
			this.Super('init', arguments);
			//继承展示资质的方法
			Object.assign(this,auditUtil.showAptitudeData);
		},
		renderId: function(i,val,data,con){
			var html = $('<div class="text-truncate"/>').text(val);
			// 双击出现复制按钮
			this.uiBind(html, 'dblclick', function(evt, elm){
				pubjs.clipboard(html);
			});
			return html;
		},
		renderName:function(i,val,data,con){
			var html = $('<label title="'+ val + '" class="text-truncate dib">').text(val);
			// 双击出现复制按钮
			this.uiBind(html, 'dblclick', function(evt, elm){
				pubjs.clipboard(html);
			});
			return html;
		},
		renderSweetyName:function(i,val,data,con){
			return val ?  $('<a target="_blank" href="' + data.SweetyThumb + '" "title="'+ val + '" class="text-truncate dib">').text(val) :"不属于任何创意包";
		},
		renderOp:function(i,val,data,con){
			var $btn = $("<button class='auditBtn'>审核</button>");
			$btn.data(data);
			this.uiBind($btn,'click','eventAuditCreative');
			return $btn;
		},
		renderCreativeName:function(i,val,data,con){
			var $previewBtn = $(`<a class='preview text-truncate dib' title='${val}' data-origin='${data.Thumb}'>${val}</a>`);
			$previewBtn.data(data);
			if (data.Type == 3 || data.youku_html){
				//外链类型打开新
				$previewBtn.attr({'href':data.youku_html || data.Path,target:'blank'});
			} else {
				this.uiBind($previewBtn,'click imageLoad imageError','_showPreviewImage');
			}
			return $previewBtn.width(280);
		},
		renderStatus:function(i,val,data,con){
			var color = 'grey';
			var text = '未知状态';
			switch (data.Status){
				case 3:
					//通过
					text = '通过';
					color = "green";
					break;
				case 2:
					text = '部分审核中';
					color = "blue";
					break;
				case 1:
					text = '全部审核中';
					color = "blue";
					break;
				case -1:
					text = '拒绝';
					color = 'red';
					break;
				case -2:
					text = '部分拒绝';
					color = 'red';
					break;
				case -3:
					text = '预审审核中';
					color = 'grey';
					break;
			}

			var html = $(`<font color='${color}'><b>${text}</b><span class='Info'></font>`);
			if (data.RejectReason){
				html.find('span.Info').html('(' +data.RejectReason + ')');
			}

			return html ;
		},
		renderSubStatus:function(index,val,data,con){
			var html = '';
			var color_map = {
				'1': 'green'
				,'-1': 'red'
			};


			if(!data.AuditList){
				data.AuditList = [];
			}
			var infoSplitLength = 30;
			var splitInfo = function (str,splitLength) {
				splitLength = splitLength || infoSplitLength;
				return str.length >splitLength ?str.substr(0,splitLength) + '......' :str;
			}

			if(data.youku_info) {
				var info = data.youku_info;
				html += `<font color='red'>
							<b>本地素材上传优酷状态:</b>
							<span >${info.message}</span>
						</font><br/>`;
			} else {
				for(var i = 0 ;i < data.AuditList.length; i++){
					var audit = data.AuditList[i];
					audit.PreInfo = (audit.PreStatus == -1 ? audit.PreInfo || '' :null)
					var whiskyUrl = pubjs.config('materialPreview/whisky').replace('{id}', audit.WhiskyId);
					var color = color_map[audit.Status] || 'blue';

					var aptitudeHtml = '';
					if(audit.Aptitude){
						aptitudeHtml = `，<a class="auditAptitudeInfo" data-index="${index}" data-creativeId="${data._id}" style="color:${color}"
 								data-aptitudeId="${audit.Aptitude.AptitudeId}">资质ID:${audit.Aptitude.AptitudeId}</a>`;
					}

					var $status = `<div style="color:${color};${i < data.AuditList.length-1 ? "border-bottom:1px dotted #ccc":''};">
									<div style="display:table-cell;vertical-align: middle;color:${color}">
										${audit.ExchangeName || '未知渠道:'+ audit.ExchangeId}
										(<a target="_blank" href="${whiskyUrl}" style="color:${color}">
											WhiskyId:${audit.WhiskyId}
											${audit.AdxDealId ? ',AdxDealId:' + audit.AdxDealId: ''}
										</a>
										${aptitudeHtml}
										)
									</div>
									<div style="display:table-cell">
										<span style="display:${audit.Info ? "inline" :"none"}">${audit.Info ? splitInfo(audit.Info) :""}</span>
										<a class="auditInfoDetail ml10" style="display:${audit.Info && audit.Info.length > infoSplitLength?'inline':'none'};" data-detail="${audit.Info || ''}">详情</a>
										${audit.Info ? "<br />" :""}
										<span style="color:#999">${audit.PreStatus == -1? splitInfo(audit.PreInfo):(audit.PreStatus == -2?"预审审核中":'')}</span>
										<a class="auditInfoDetail ml10" style="display:${audit.PreStatus == -1 && audit.PreInfo && audit.PreInfo.length > infoSplitLength?'inline':'none'};" data-detail="${audit.PreInfo || ''}">详情</a>
									</div>
								</div>`;

					html += $status;
				}

			}


			var $html = $(html);
			this.uiBind($html.find('a.auditInfoDetail'),'click','showAuditInfo');
			this.uiBind($html.find('a.auditAptitudeInfo'),'click','showAptitudeInfo');
			return $html ;
		}
		,renderTime: function(i, val) {
			return val ? util.date("Y-m-d H:i:s", val) : "";
		},
		showAuditInfo:function (ev) {
			var el = $(ev.target);
			var html = $(el).data('detail');
			auditUtil.showInfo.call(this,'previewInfo',ev,html);
		},
		showAptitudeInfo:function (ev) {
			var el = $(ev.target);
			this.$showAptitudeInfoEventTarget = ev;
			var creativeId = $(el).data('creativeid');
			var aptitudeId = $(el).data('aptitudeid');
			this.$showAptitudeRowIndex = +$(el).data('index');
			this.getAptitudeData(creativeId,aptitudeId);
		},
		eventAuditCreative:function(e){
			var data = $(e.target).data();
			this.fire('openAuditPopwin',data);
		},
		_showPreviewImage:function(ev){
			auditUtil.showPreviewImage.call(this,ev);
		},
		onRefreshManual: function(){
			// 手动刷新，清除选中的创意
			this.setSelectRowIds([]);
			this.clearAptitudeDataCache();
			this.load();
			return false;
		}
	});

	exports.base = HighGrid;

});